from typing import Optional
import flet as ft
import win32gui

class Page(ft.Page):
    first: int
    bonus: bool

def init_page(page: Page):
    page.first = 1

# 显示提示信息
def show_snack_bar(page, text, color, selectable=False):
    snack_bar = ft.SnackBar(
        content=ft.Text(text, selectable=selectable),
        bgcolor=color,
    )
    # 添加并立即显示
    page.overlay.append(snack_bar)
    snack_bar.open = True
    page.update()
    return snack_bar

def cleanup():
    try:
        win32gui.ShowWindow(mycmd, 1)
    except:
        pass

def enum_windows_callback(hwnd, hwnds):
    class_name = win32gui.GetClassName(hwnd)
    name = win32gui.GetWindowText(hwnd)
    try:
        if (
            class_name == "ConsoleWindowClass"
            and win32gui.IsWindowVisible(hwnd)
            and "gui" in name[-7:]
        ):
            hwnds.append(hwnd)
    except:
        pass
    return True


def list_handles():
    hwnds = []
    win32gui.EnumWindows(enum_windows_callback, hwnds)
    hwnds.append(0)
    return hwnds

mycmd = list_handles()[0]